const app = getApp();
const req = require("../../utils/http.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    page: 1,
    size: 15,
    total: '',
    newList: [], //列表
    statistics: [],
    showModalStatus: false,
    headerTime:'',
    typeId: '0',
    fields:'day',
    payType : '',
    index:'0',
    showTime:false,
    storeId: '', //门店id
    billId:'0',//账单ID
    isPickerRender: false, //时间弹框
    isPickerShow: false,
    array: [],
    params: {},
    current: '0', 
    orderStatus:'',
  
  },



  // ----详情列表------
  transaction(page, size, params) {
    let t = this
    req.wxReq(`/api/statistics/getStoreOrderGroupList/${page}/${size}`, "GET", params).then(res => {
      let {
        list,
        statistics
      } = t.data
      let result = res.data.result
      if (res.data.code == 200) {
        list = result.page.records
        let newList = t.data.newList.concat(list)
        t.setData({
          newList,
          statistics: result.statistics,
          total: result.page.total,
          showModalStatus: false
        })
      } else {
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 500
        })
      }
    })
  },

  // --点击查询---
  filtrateClik(e) {
    let t = this
    let page = 1
    let params = e.detail.params
    let size = t.data.size
    t.setData({
      params,
      newList:[],
    })

    t.transaction(page, size, params)

  },

  // --跳转详情页---
  detailsClick(e){
   let t = this
    let storeId = e.currentTarget.dataset.id
    let params = JSON.stringify(t.data.params)
    wx.navigateTo({
      url: '../transaction_01/transaction_01?storeId=' + storeId+'&params='+params,
    })

  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var windowHeight = wx.getSystemInfoSync().windowHeight
    let t = this
    if (wx.getStorageSync('token')){ //已登录
      let {
        page,
        size,
        newList:[]
      } = t.data
  
      wx.setNavigationBarTitle({
        title: app.globalData.name
      })
      let myData = new Date();
      let getFullYear = myData.getFullYear();
      let getMonth = myData.getMonth() + 1;
      let getDate = myData.getDate();
      if (getDate < 10) {
        getDate = '0' + getDate
      }
      if (getMonth < 10) {
        getMonth = '0' + getMonth
      }
      let currentDate = getFullYear + '-' + getMonth + '-' + getDate; //年月日
      let currentMonth = getFullYear + '-' + getMonth  //年月
      let currentYear = getFullYear //年

      let params = {
        queryTime: currentDate,
        quertType:'0',
        orderType:'0'
      }
      t.transaction(page, size, params)
  
      t.setData({
        date: currentDate,
        endDate: currentDate,
        username: wx.getStorageSync("username"),
        type: options.type,
        itemDate: currentDate,
        currentMonth,
        currentYear,
        currentDate,
        params,
        scrollHight: windowHeight - 219
      })
    }else{ //未登录
      wx.showToast({
        title: '您还未登录，请先登录！',
        icon:'none',
        duration:500
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '../index/index',
        })
      }, 700)   
    }
  
  },



  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading()
    let t = this
    let page = 1
    let {
      // page,
      size,
      params
    } = t.data
    
    if (this.data.params) {
      t.setData({
        newList: []
      })
    }
  
    t.transaction(page, size, params)
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      wx.stopPullDownRefresh()
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    console.log('shangl')
    let t = this
    var page = t.data.page + 1;
    t.setData({
      page,

    })
    var page = t.data.page;
    let params = t.data.params
    if (page > Math.ceil(this.data.total / this.data.size)) {
      wx.showToast({
        title: '到底了！！！',
        icon: 'none'
      })
      return;
    };
   
    this.transaction(page, this.data.size, params)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})